var ns, map;
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        dataList: [],
        xiaoquname: '',
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getallxiaoqulist'
            }, function(succeed, data, err) {
                if (succeed) {
                    that.dataList = data.list;
                    that.list = data.list;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                is_getapi: true
            });
        },
        do_choose: function(item) {
            var that = this;
            var uid = ns.getPrefsVaue('uid');
            that.xiaoquname = item.name;
            that.do_set_chosen_value(item);
        },
        open_search_bar: function() {
            var that = this;
            that.list = [];
            var avalue = $api.byId("search-input").value;
            for (i = 0; i < that.dataList.length; i++) {
                if (that.dataList[i].name.indexOf(avalue) > -1) {
                    that.list.push(that.dataList[i]);
                }
            }
        },
        do_set_chosen_value: function(item) {
            var that = this;
            var my_chosen_xiaoquid = api.getPrefs({
                sync: true,
                key: 'my_chosen_xiaoquid'
            });
            api.setPrefs({
                key: 'my_chosen_xiaoquname',
                value: item.name
            });
            api.setPrefs({
                key: 'my_chosen_xiaoquid',
                value: item.id
            });
            api.sendEvent({
                name: 'do_choose_xiaoqu_done',
                extra: {
                    id: item.id,
                    name: item.name
                }
            });
            api.closeWin();
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.get_data();
    api.sendEvent({
        name: 'closehomeintro',
    });
    //键盘监听事件
    document.onkeydown = function(e) {
        let key = window.event.keycode;
        app.open_search_bar();
    };
};
